package KuangShen.ZhuJieHeFanShe;

import java.lang.annotation.*;
import java.lang.reflect.Field;

/*
了解什么是ORM
    1.Object relationship Mapping--->对象关系映射
    类和表结构对应
    属性和字段对应
    对象和记录对应---------->每new（）一个对象相当于在数据库的表中建立了一列记录
    利用注解和反射完成类和表结构的映射关系
 */
//练习反射操作注解
public class test12 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("KuangShen.ZhuJieHeFanShe.Student2");

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得指定注解的value值
        TableStudent tableStudent =(TableStudent)c1.getAnnotation(TableStudent.class);
        String value =tableStudent.value();
        System.out.println(value);


        //获得类指定的注解
        Field field = c1.getDeclaredField("name");
        FieldStudent annotation = field.getAnnotation(FieldStudent.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());

    }
}

//创建简单的实体类
@TableStudent("db_student")
class Student2{
    @FieldStudent(columnName="db_Studentid",type = "int",length =10)
    private int id;
    @FieldStudent(columnName="db_Studentage",type = "int",length =10)
    private int age;
    @FieldStudent(columnName="db_Studentname",type = "varchar",length =5)
    private String name;


    public Student2() {
    }


    public Student2(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//给类名创建一个注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableStudent{
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldStudent{
    String columnName();//列名
    String type();
    int length();
}